package moocloudstudio.minder.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import moocloudstudio.minder.entity.MapShare;
import moocloudstudio.minder.entity.MinderMap;
import moocloudstudio.minder.entity.MinderUser;
import moocloudstudio.minder.enums.BooleanEnum;
import moocloudstudio.minder.exception.NotFoundException;
import moocloudstudio.minder.repository.MapRepository;
import moocloudstudio.minder.service.MapService;
import moocloudstudio.minder.service.UserService;
import moocloudstudio.minder.util.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class MapServiceImpl implements MapService {
    @Autowired
    private UserService userService;
    @Autowired
    private MapRepository mapRepository;

    @Override
    public MinderMap findAndCheckById(String mapId) {
        MinderMap map = mapRepository.findById(mapId).orElseThrow(()->new NotFoundException("思维导图不存在"));
        if(BooleanEnum.True.getValue().equals(map.getDeleted())){
            throw new NotFoundException("思维导图不存在");
        }
        return map;
    }

    @Override
    public MinderMap save(MinderMap map) {
        // 检查用户
        userService.findAndCheckById(map.getUserId());

        if (StrUtil.isBlank(map.getId())){
            // 新增
            EntityUtil.setCreateMetaFields(map);
            map.setMapShare(MapShare.createShare());
        } else {
            // 更新脑图
            MinderMap oldMap = findAndCheckById(map.getId());
            map.setCreateBy(oldMap.getCreateBy());
            map.setCreatedAt(oldMap.getCreatedAt());
            map.setUpdatedAt(LocalDateTime.now());
            map.setMapShare(oldMap.getMapShare());
        }
        mapRepository.save(map);
        return map;
    }

    @Override
    public List<MinderMap> list(String userId) {
        userService.findAndCheckById(userId);
        MinderMap queryMap = new MinderMap();
        queryMap.setUserId(userId);
        queryMap.setDeleted(BooleanEnum.False.getValue());
        return  mapRepository.findAll(Example.of(queryMap));
    }

    @Override
    public void logicDeleteById(String mapId) {
        MinderMap map = findAndCheckById(mapId);
        MinderUser user = userService.findAndCheckById(map.getUserId());
        map.setDeleted(BooleanEnum.True.getValue());
        map.setUpdateBy(user.getUsername());
        map.setUpdatedAt(LocalDateTime.now());
        mapRepository.save(map);
    }

    @Override
    public void deleteById(String mapId) {
        MinderMap map = findAndCheckById(mapId);
        if (BooleanEnum.False.getValue().equals(map.getDeleted())){
            throw new RuntimeException("无法删除此思维导图");
        }
        mapRepository.deleteById(mapId);
    }

    @Override
    public MinderMap getAndCheckMapByShareId(String shareId) {
        MinderMap queryMap = new MinderMap();
        queryMap.setDeleted(BooleanEnum.False.getValue());
        MapShare queryShare = new MapShare();
        queryShare.setShareId(shareId);
        queryMap.setMapShare(queryShare);
        MinderMap resultMap = mapRepository.findOne(Example.of(queryMap)).orElseThrow(() -> new NotFoundException("分享链接不存在"));
        if (BooleanEnum.True.getValue().equals(resultMap.getDeleted())){
            throw new NotFoundException("思维导图不存在");
        }
        return resultMap;
    }

    @Override
    public MinderMap updateShared(String shareId, Boolean shared, String updateBy) {
        MinderMap map = getAndCheckMapByShareId(shareId);
        map.setUpdatedAt(LocalDateTime.now());
        map.setUpdateBy(updateBy);
        map.getMapShare().setShared(shared);

        mapRepository.save(map);
        return map;
    }
}
